﻿package njClass.UI{
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class PlusToolTip
	{
		private static var toolTip : TextField;
		private static var format : TextFormat;
		private static var owner :DisplayObjectContainer;
		public static var enabled : Boolean = true;
		private static var owners : Array = new  Array();
		private static var texts : Array = new Array();
		
		public function PlusToolTip() {
			
		}
		
		public static function get root() :DisplayObjectContainer{
			return owner;
		}
		
		public static function set root(value :DisplayObjectContainer) : void {
			if (owner == null){
				owner = value;
			}
		}
		
		public static function create(owner:InteractiveObject, text: String) : void {
		 	if(owners.indexOf(owner)==-1){
		 		owners.push(owner);
				texts.push(text);
		 	}else{
		 		texts[owners.indexOf(owner)]=text;
		 	}
		 	owner.addEventListener(MouseEvent.MOUSE_OVER, showToolTip);
		}
		
		private static function showToolTip(e : MouseEvent) : void {
			var toolTip_mc:Sprite=new Sprite;
			toolTip = new TextField();
			toolTip.visible = true;
			toolTip.width=200;
			toolTip.multiline=true;
			toolTip.wordWrap=true;
			toolTip.y=3;
			toolTip.text = findText(InteractiveObject(e.currentTarget));
			toolTip.autoSize = TextFieldAutoSize.CENTER;
			format = new TextFormat();
			format.font = "宋体";
			format.leftMargin = 4;
			format.rightMargin = 4;
			format.size = 13;
			format.leading=5;
			format.indent=24;
			toolTip.setTextFormat(format);
			toolTip_mc.addChild(doDraw(200,toolTip.textHeight+10,0xFFFFBB));
			toolTip_mc.addChild(toolTip);
			if(root.mouseX+20+toolTip_mc.width>=root.width){
				toolTip_mc.x=root.mouseX-toolTip_mc.width-20;
			}else{
				toolTip_mc.x=root.mouseX+20;
			}
			if(root.mouseY+20+toolTip_mc.height>=root.height){
				toolTip_mc.y=root.mouseY-toolTip_mc.height-20;
			}else{
				toolTip_mc.y=root.mouseY+20;
			}
			
			root.addChild(toolTip_mc);
			owner.addEventListener(MouseEvent.MOUSE_OUT, hideToolTip);
			
			function hideToolTip(e : MouseEvent) : void {
				root.removeChild(toolTip_mc);
				owner.removeEventListener(MouseEvent.MOUSE_OVER, showToolTip);
				owner.removeEventListener(MouseEvent.MOUSE_OUT, hideToolTip);
			}
		}
		
		private static function doDraw(w:Number,h:Number,bgColor:uint):Shape{
			var temp_shape:Shape=new Shape();
			temp_shape.graphics.beginFill(bgColor,0.8);
			temp_shape.graphics.lineStyle(2,0x666666);
			temp_shape.graphics.drawRoundRect(0,0,w,h,10);
			temp_shape.graphics.endFill();
			return temp_shape;
		}
		
		private static function findText(owner:InteractiveObject) : String {
			var index : int = owners.indexOf(owner);
			return texts[index];
		}
	}
}